import json
import os
import sys
import threading

import setting
import requests
from PyQt5.Qt import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from auto_tft import AutoTFT
from moni.hook_key_mouse import HookKeyMouse
from moni.moni import MoNi
from page.page_doc import FormDoc
from page.page_doc_switch import FormDocSwitch
from page.page_early import FormEarly
from page.page_equip import FormEquip
from page.page_job_and_race import FormJobAndRace
from page.page_location import FormLocation
from page.page_nav import FormNav
from page.page_show_log import ShowLog
from page.page_strategy_list import FormStrategyList
from page.page_team import FormTeam
from tft import  tft
from tools.utils import CreateNewImage
from ui.main import Ui_MainWindow
class MySignals(QObject):
    hide_ui = pyqtSignal()
    show_log = pyqtSignal(str,tuple,int)
    switch_id=pyqtSignal(bool)
    update_ui = pyqtSignal(int,int)
class main(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.set_ui()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Widget | Qt.WindowStaysOnTopHint)#风格
        self.setAttribute(Qt.WA_TranslucentBackground)  # 设置窗口背景透明
        #self.setWindowOpacity(1.0)#设置1-0之间设置窗口透明度
        self.setWindowIcon(QIcon("datas/img/logo.png"))
        # 保持全屏置顶
        self.setGeometry(0, 0, setting.window_width, setting.window_height)
        self.setWindowTitle('LRTFT' + setting.ver)  # 标题
        self.strategy=None
        # 子窗口
        self.window_nav = FormNav(self)
        self.window_team = FormTeam(self)

        self.window_strategy_list = FormStrategyList(self)
        self.window_job_and_race=FormJobAndRace(self)
        self.window_equip=FormEquip(self)
        self.window_doc = FormDoc(self)
        self.window_doc_switch=FormDocSwitch(self)
        self.window_location=FormLocation(self)
        self.window_early=FormEarly(self)
        self.tft = tft
        self.down_imgs()
        self.load_strategy_list()
        # 设置字体大小
        self.setStyleSheet(
            "font-family: {}; font-size: {}pt;".format(setting.font.family(), setting.font.pointSize()))
        self.sg=MySignals()
        self.sg.hide_ui.connect(self.on_hide_ui)
        self.sg.show_log.connect(self.on_show_log)
        self.sg.switch_id.connect(self.on_switch_id)
        self.sg.update_ui.connect(self.update_ui)

        moni = MoNi().create(0, 2)
        self.auto_tft = AutoTFT(moni)
        self.auto_tft.set_sg(self.sg)
        self.auto_tft.get_game_hwnd()
        # 启动全局钩子
        HookKeyMouse.start_global_hook()
        self.thread_auto_tft=threading.Thread(target=self.auto_tft.run,daemon=True)
        self.thread_auto_tft.start()


        self.show_log=None
        self.load_strategl_old_idex()


    def set_ui(self):
        pass
    @pyqtSlot()
    def on_hide_ui(self):
        try:
            on=self.isMinimized()
            if not on:
                self.showMinimized()
                self.on_show_log("隐藏UI",(255,255,0),1000)
            else:
                self.showNormal()
                self.on_show_log("显示UI",(255,255,0),1000)
        except Exception as e:
            print(e)
            pass
    @pyqtSlot(str,tuple,int)
    def on_show_log(self, log: str,color=[0,0,0],time_end=1000):
        try:
            self.show_log = ShowLog(log,time_end=time_end,color=color)
            self.show_log.show()
        except Exception as e:
            print(e)
            pass
    @pyqtSlot(bool)
    def on_switch_id(self,on:bool):
        row = self.window_strategy_list.tw_strategy_list.currentIndex().row()
        if on:
            next_row = row+ 1
        else:
            next_row = row - 1
        if next_row < 0:
            next_row = 0
        if next_row >= self.window_strategy_list.tw_strategy_list.rowCount():
            next_row = self.window_strategy_list.tw_strategy_list.rowCount() - 1
        self.window_strategy_list.tw_strategy_list.setCurrentCell(next_row, 0)

    @pyqtSlot(int,int)
    def update_ui(self,p_left=0,p_top=0):
        '''
        更新ui
        :return:
        '''
        # 保持全屏置顶
        self.setGeometry(p_left, p_top, setting.window_width, setting.window_height)
        # 设置字体大小
        self.setStyleSheet(
            "font-family: {}; font-size: {}pt;".format(setting.font.family(), setting.font.pointSize()))
        self.sg = MySignals()

        # 成员列表展示
        self.window_team.move(0, 0)


        self.window_team.resize(int(setting.ratio * 740), int(setting.ratio * 100))
        # 导航栏
        self.window_nav.move(int(setting.ratio * 740)-2, 0)
        self.window_nav.resize(int(setting.ratio * 224), int(setting.ratio * 100))

        # 攻略列表
        self.window_strategy_list.move(0, int(setting.ratio * 100)-2)
        self.window_strategy_list.resize(int(setting.ratio * 550), int(setting.ratio * 260))

        # 设置默认行高为55像素
        self.window_strategy_list.tw_strategy_list.verticalHeader().setDefaultSectionSize(int(setting.ratio * 55))
        self.window_strategy_list.tw_strategy_list.horizontalHeader().setDefaultSectionSize(int(setting.ratio * 505))

        #职业和羁绊展示窗口
        self.window_job_and_race.move(int(setting.ratio * 550),int(setting.ratio * 100)-2)
        self.window_job_and_race.resize(int(setting.ratio * 190), int(setting.ratio * 260))
        self.window_job_and_race.setMaximumSize(int(setting.ratio * 190), int(setting.ratio * 260))

        #装备推荐窗口
        self.window_equip.resize(int(setting.ratio * 344), int(setting.ratio * 440))
        self.window_equip.move(p_left, p_top+self.height()-self.window_equip.height())

        # 攻略文案窗口
        self.window_doc.resize(int(setting.ratio * 800), int(setting.ratio * 176))
        self.window_doc.setMaximumSize(int(setting.ratio * 800), int(setting.ratio * 176))
        self.window_doc.move(p_left+self.width()-self.window_doc.width()-int(setting.ratio * 68), p_top+int(setting.ratio * 60))

        #切换攻略窗口导航
        self.window_doc_switch.resize(int(setting.ratio * 210), int(setting.ratio * 58))

        self.window_doc_switch.move(self.width() - self.window_doc_switch.width() - int(setting.ratio * 68), 0)

        #阵容站位
        self.window_location.resize(int(setting.ratio * 570), int(setting.ratio * 316))
        self.window_location.move(p_left+self.width() - self.window_location.width() ,p_top+self.height() - self.window_location.height() )

        # 阵容站位
        self.window_early.resize(int(setting.ratio * 570), int(setting.ratio * 316))
        self.window_early.move(p_left+self.width() - self.window_early.width(),
                                  p_top+self.height() - self.window_early.height())
        self.window_early.setVisible(False)
        self.window_strategy_list.setVisible(False)
        self.window_job_and_race.setVisible(False)
        self.window_doc.setVisible(False)
    def load_strategy_list(self):
        """
        加载官方阵容
        """
        self.window_strategy_list.set_teams(self.tft.strategy_list)
    def down_imgs(self):
        '''
        下载官网的图片数据
        :return:
        '''
        # '棋子图片'
        for item in self.tft.chess_list:
            name = item['name']
            Path_1 = f"{setting.Path_chess}/{name}"
            if os.path.exists(Path_1) == False:
                url = f"{setting.chess_url}{name}"
                res = requests.get(url, verify=False)
                with open(Path_1, 'wb') as f:
                    f.write(res.content)

        # '棋子大图'
        for item in self.tft.chess_list:
            name = item['name'].split('.')[0]
            Path_1 = f"{setting.Path_chess_big}/{name}.jpg"
            if os.path.exists(Path_1) == False:
                url = f"{setting.chess_big_url}{name}.jpg"
                res = requests.get(url)

                with open(Path_1, 'wb') as f:
                    f.write(res.content)

        # '装备图片'
        for item in self.tft.equip_list:
            name = item['imagePath'].split('/')[-1]
            Path_1 = f"{setting.Path_equip}/{name}"
            if os.path.exists(Path_1) == False:
                url = item['imagePath']
                url = url.replace("https", "http")
                res = requests.get(url, verify=False)

                with open(Path_1, 'wb') as f:
                    f.write(res.content)

        # '职业图片'
        for item in self.tft.job_list:
            try:

                name = item['imagePath'].split('/')[-1]
                Path_1 = f"{setting.Path_job}/{name}"
                if os.path.exists(Path_1) == False:
                    if item['name'] == "召唤物":
                        continue
                    url = item['imagePath']
                    url = url.replace("https", "http")
                    res = requests.get(url)
                    Path_2 = f"{setting.Path_job2}/{name}"
                    with open(Path_2, 'wb') as f:
                        f.write(res.content)
                    # 将图片的颜色转换成白色
                    CreateNewImage(Path_2, Path_1, [255, 255, 255])

            except Exception as err:
                print("职业图片下载失败", err,item)

        #'羁绊图片'
        for item in self.tft.race_list:
            try:
                name = item['imagePath'].split('/')[-1]
                Path_1 = f"{setting.Path_race}/{name}"
                if not os.path.exists(Path_1):
                    if item['name'] == "召唤物":
                        continue
                    url = item['imagePath']
                    url = url.replace("https", "http")
                    res = requests.get(url, verify=False)

                    Path_2 = f"{setting.Path_race2}/{name}"
                    with open(Path_2, 'wb') as f:
                        f.write(res.content)
                    # 将图片的颜色转换成白色
                    CreateNewImage(Path_2, Path_1, [255, 255, 255])
            except Exception as err:
                print("羁绊图片下载失败", err,item)
    def load_strategl_old_idex(self):
        try:
            with open('datas/setting.json', 'r', encoding='utf-8') as f:
                loaded_data = json.load(f)
                # 定位到最新的索引
                self.window_strategy_list.tw_strategy_list.setCurrentCell(loaded_data['idex'], 0)
                #self.window_strategy_list.tw_strategy_list.scrollToItem(self.window_strategy_list.tw_strategy_list.currentItem(), QAbstractItemView.PositionAtTop)
                if loaded_data['is_first']:
                    with open('必看说明.txt', 'r', encoding='utf-8') as f:

                        self.show_log = ShowLog( "30秒后自动消失    "+f.read(),time_end=30000, color=(255, 255, 0))

                else:
                    self.show_log = ShowLog("懒人TFT永久免费!欢迎使用!\n按~反撇号 开启/关闭拿牌 \n按Home键 隐藏/显示UI",
                                            time_end=2000, color=(255, 255, 0))
                self.show_log.show()

        except Exception as e:
            print(e)
    def save_strategl_old_ide(self):
        # 将数据保存到 JSON 文件
        data={}
        data['idex']=self.window_strategy_list.tw_strategy_list.currentIndex().row()
        data['is_first']=False
        with open('datas/setting.json', 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
    def closeEvent(self, event):
        self.save_strategl_old_ide()
        qApp.quit()












